package org.offsync.util

class Tasks {
  val lock = new Object()
  var tasks: List[() => Unit] = Nil
  
  def add(task: => Unit): Unit = lock.synchronized{
    tasks ::= { () => task }
  }
  
  def run(): Unit = {
    val tasksToRun = lock.synchronized { try tasks finally tasks = Nil }
    for (task <- tasksToRun.reverse) task()
  }
}
